<?php
    include_once '../connection/databaseHandler.php';
    include_once '../connection/sessionHandler.php';
    $database = new DatabaseHandler();
    $session = new SessionHandler();

    if ($_GET['ref']=="wall") {
        // mempost komentar di wall
        $susername = $_POST['username'];
        $scontent = $_POST['wall_comment_write'];
        $sdate = date("Y-m-d");
    }
    else if ($_GET['ref']=="foto") {
        // mempost komentar di foto
        $susername = $_POST['username'];
        $scontent = $_POST['foto_comment_write'];
        $sdate = date("Y-m-d");
    }
    else if ($_GET['ref']=="message") {
        // mempost komentar di message
        $susername = $_POST['username'];
        $scontent = $_POST['message_reply_write'];
        $sdate = date("Y-m-d");
    }

    // menghilangkan kemungkinan SQL Injection
    $content = stripslashes($scontent);
    $content = mysql_escape_string($scontent);

    // mengisi database komentar
    $query_c = "INSERT INTO $database->t_comment (
                id,
                username,
                content,
                date
            ) VALUES (
                NULL,
                '$susername',
                '$scontent',
                '$sdate'
            )";

    // eksekusi database
    if (!$database->execQuery($query_c)) {
        echo "<script>";
        echo "alert('Failed update comment, please try again')";
        echo "</script>";
        die();
    }

    if ($_GET['ref']=="wall") {
        // mengisi database relasi
        $swall_id = $_POST['wall_id'];

        $query_cw = "INSERT INTO $database->t_comment_wall (
                    id_wall,
                    id_comment
                ) VALUES (
                    '$swall_id',
                    LAST_INSERT_ID()
                )";

        if (!$database->execQuery($query_cw)) {
            echo "<script>";
            echo "alert('Failed link comment, please try again')";
            echo "</script>";
            die();
        }

        // redirect halaman
        if ($_GET['id']!=$session->username) {
            header("location:../profile.php?id=".$_GET['id']."&page=wall");
        }
        else {
            header("location:../profile.php?page=wall");
        }
    }
    else if ($_GET['ref']=="foto") {
        // mengisi database relasi
        $sfoto_id = $_POST['foto_id'];

        $query_cw = "INSERT INTO $database->t_comment_foto (
                    id_foto,
                    id_comment
                ) VALUES (
                    '$sfoto_id',
                    LAST_INSERT_ID()
                )";

        if (!$database->execQuery($query_cw)) {
            echo "<script>";
            echo "alert('Failed link comment, please try again')";
            echo "</script>";
            die();
        }

        // redirect halaman
        if ($_GET['id']!=$session->username) {
            header("location:../profile.php?id=".$_GET['id']."&page=photo-detail&photo-id=".$sfoto_id);
        }
        else {
            header("location:../profile.php?page=photo-detail&photo-id=".$sfoto_id);
        }
    }
    else if ($_GET['ref']=="message") {
        // mengisi database relasi
        $smessage_id = $_POST['message_id'];

        $query_cm = "INSERT INTO $database->t_comment_message (
                    id_message,
                    id_comment
                ) VALUES (
                    '$smessage_id',
                    LAST_INSERT_ID()
                )";

        if (!$database->execQuery($query_cm)) {
            echo "<script>";
            echo "alert('Failed link comment, please try again')";
            echo "</script>";
            die();
        }

        // redirect halaman
        if ($_GET['id']!=$session->username) {
            header("location:../profile.php?id=".$_GET['id']."&page=show-message&mid=".$smessage_id."");
        }
        else {
            header("location:../profile.php?page=show-message&mid=".$smessage_id."");
        }
    }    
?>
